{include file="public/header"}
<!-- 公共样式 结束 -->
<style>
    .layui-table img {
        max-width: none;
    }
</style>
</head>

<body>
    <div class="cBody">
        <div class="console">
            <a class="layui-btn" onclick="OpenView('{:url('dbd/dbdadd')}','添加数据表')">添加数据表</a>
            <a class="layui-btn layui-btn-warm" onclick="restore({})">还原数据表</a>
        </div>
        <table id="demo" lay-filter="test"></table>
    </div>
</body>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="detail">查看字段</a>
    <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="backups">备份</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script type="text/html" id="barBackups">
    {{#  if(d.is_backups =='1'){ }}
    <div lay-event="download"><a href="{:url('Dbd/download')}?name={{d.Name}}" style="color: #FFB800;"><i class="layui-icon layui-icon-export"></i>已备份</a></div>
    {{#  } else { }}
    <div>未备份</div>
    {{#  } }}
</script>
<script>
    layui.use(['table', 'layer'], function () {
        var table = layui.table;
        var layer = layui.layer;
        //第一个实例
        var datatable = table.render({
            elem: '#demo'
            , url: '?page=page' //数据接口
            , cols: [[ //表头
                { field: 'Name', title: '数据表名', sort: true },
                { field: 'Rows', title: '记录条数', sort: true },
                { field: 'Size', title: '占用空间', sort: true },
                { field: 'Create_time', title: '创建时间', sort: true },
                { field: 'Update_time', title: '更新时间', sort: true },
                { field: 'Comment', title: '数据注释', sort: false },
                { field: 'is_backups', title: '状态', sort: false, templet: "#barBackups" },
                { field: 'right', title: '操作', toolbar: "#barDemo", width: "200" }
            ]]
        });
        //监听工具条
        table.on('tool(test)', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('删除了无法恢复的哦！确定吗？', function (index) {
                    // console.log(data);
                    if (data.is_backups == '1') {
                        $.ajax({
                            url: '{:url("dbd/dbddel")}',
                            dataType: 'json',
                            type: 'GET',
                            data: { 'name': data.Name },
                            success: function (res) {
                                console.log(res);
                                if (res.code == 0) {
                                    obj.del();
                                    layer.close(index);
                                } else {
                                    layer.msg(res.msg, {
                                        icon: 2
                                    });
                                }
                            }, error: function () {
                                layer.msg('网络出错，请联系客服！', {
                                    icon: 0
                                });
                            }
                        });
                    } else {
                        layer.msg('请先备份！', {
                            icon: 2
                        });
                    }
                });
            } else if (obj.event === 'detail') {
                $(window).attr('location', '{:url("dbd/fieldlist")}?name=' + data.Name);
            } else if (obj.event === 'backups') {
                var name = data.Name;
                backup({ "backdir": [name] });
            }
        });
    });
    //数据库备份
    function backup(predate) {
        layui.use(['table', 'layer'], function () {
            var layer = layui.layer;
            var table = layui.table;
            $.ajax({
                type: 'POST', //POST
                url: "{:url('Dbd/dbdbackups')}",
                async: false, //或false,是否异步
                dataType: 'json',
                data: predate,
                success: function (data, textStatus, jqXHR) {
                    // console.log(data);
                    if (data.totalpercentage < 100) {
                        backup(data);
                    } else {
                        layer.msg('已备份');
                        table.reload('demo');
                    }
                },
                error: function () {
                    layer.msg('网络出错，请联系客服！', {
                        icon: 0
                    });
                }
            });
        });
    }
    //还原已备份的数据表
    function restore(predate) {
        layui.use(['layer'], function () {
            var layer = layui.layer;
            $.ajax({
                url: "{:url('Dbd/restore')}",
                type: 'POST', //GET
                async: false, //或false,是否异步
                data: predate,
                dataType: 'json',
                success: function (data, textStatus, jqXHR) {
                    // console.log(data);
                    if (data.totalpercentage < 100) {
                        datainfo = '正在恢复...';
                    } else {
                        datainfo = '恢复完成';
                    }
                    layer.msg(datainfo);
                    if (data.totalpercentage < 100) {
                        restore(data);
                    }
                }
            });
        });

    }
</script>
{include file="public/footer"}